package com.koron.order.repair.service.impl;

import com.koron.bean.base.Response;
import com.koron.bean.standard.jobStock.repair.dto.RepairDTO;
import com.koron.bean.standard.jobStock.repair.dto.RepairItemDTO;
import com.koron.bean.standard.jobStock.repair.po.RepairItemParams;
import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderExecute;
import com.koron.order.common.bean.po.OrderExecutePart;
import com.koron.order.common.bean.po.OrderInstructionBean;
import com.koron.order.common.bean.po.OrderSignBean;
import com.koron.order.common.service.OrderExecutePartService;
import com.koron.order.common.service.OrderExecuteService;
import com.koron.order.common.service.OrderInstructionService;
import com.koron.order.common.service.OrderSignService;
import com.koron.order.repair.bean.convertor.RepairConvertor;
import com.koron.order.repair.bean.entity.*;
import com.koron.order.repair.bean.vo.*;
import com.koron.order.repair.feign.RepairStandService;
import com.koron.order.repair.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RepairExecuteServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/9/16 8:53
 */
@Service("repairExecuteService")
public class RepairExecuteServiceImpl implements RepairExecuteService {

    @Autowired
    private OrderExecuteService orderExecuteService;

    @Autowired
    private OrderExecutePartService orderExecutePartService;

    @Autowired
    private RepairReadyService repairReadyService;

    @Autowired
    private RepairEquipmentService equipmentService;

    @Autowired
    private RepairItemService itemService;

    @Autowired
    private RepairItemParamsService itemParamsService;

    @Autowired
    private RepairService repairService;

    @Autowired
    RepairConvertor repairConvertor;

    @Autowired
    OrderSignService orderSignService;

    @Autowired
    private OrderInstructionService orderInstructionService;

    @Autowired
    RepairStandService repairStandService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, RepairExecuteVo executeVo) {

        String orderId = executeVo.getOrderId();

        List<OrderExecute> orderExecuteList = new ArrayList<>();
        List<OrderExecutePart> orderExecutePartList = new ArrayList<>();
        List<RepairReadyPo> repairReadyPoList = new ArrayList<>();

        //1.作业表单信息
        OperateFormVo operateFormVo = executeVo.getOperateFormVo();
        //1.2 作业表单-存在问题,改进建议 入主表
        String opinion = operateFormVo.getOpinion();
        String problem = operateFormVo.getProblem();
        //1.3 作业表单-作业准备 根据id更新确认状态值
        List<RepairReadyPo> readyBeanList = operateFormVo.getRepairReadyPoList();
        repairReadyPoList.forEach(p -> {
            RepairReadyPo repairReadyPo = new RepairReadyPo();
            Integer result = p.getResult();
            repairReadyPo.setId(p.getId());
            repairReadyPo.setResult(result);
            repairReadyPoList.add(repairReadyPo);
        });
        repairReadyService.updateBatch(factory, repairReadyPoList);

        List<RepairItemVo> itemBeanVoList = operateFormVo.getItemBeanVoList();
        itemBeanVoList.forEach(p -> {
            List<RepairItemParamsPo> itemParamsBeanList = p.getItemParamsBeanList();
            if (!CollectionUtils.isEmpty(itemParamsBeanList)) {
                itemParamsBeanList.forEach(t -> {
                    //作业过程维护参数
                });
            }
        });

        //2.正常维护执行信息
        List<RepairExecuteInfoVo> repairExecuteInfoVos = executeVo.getRepairExecuteInfoVos();

        repairExecuteInfoVos.forEach(p -> {

            //2.1工单执行信息
            //OrderExecute orderExecute = p.getOrderExecute();
            OrderExecute orderExecute = new OrderExecute();
            String executeId = StringUtils.isEmpty(orderExecute.getId()) ? CodeTools.getCode32() : orderExecute.getId();
            orderExecute.setId(executeId);
            orderExecute.setOrderId(orderId);
            orderExecuteList.add(orderExecute);

            //2.2工单执行备件信息
            List<OrderExecutePart> orderExecutePart = p.getOrderExecutePart();
            if (!CollectionUtils.isEmpty(orderExecutePart)) {
                orderExecutePart.forEach(e -> {
                    e.setExecuteId(executeId);
                    e.setOrderId(orderId);
                });
                orderExecutePartList.addAll(p.getOrderExecutePart());
            }

        });

        orderExecuteService.insertBatch(factory, orderExecuteList);
        orderExecutePartService.insertBatch(factory, orderExecutePartList);

        return 1;
    }

    @Override
    @TaskAnnotation("insertOrUpdateExecuteInfo")
    public String insertOrUpdateExecuteInfo(SessionFactory factory, RepairExecuteInfoVo repairExecuteInfoVo) {

        //转换成工单执行信息
        OrderExecute orderExecute = repairConvertor.voToOrderExecuteVo(repairExecuteInfoVo);

        String orderExecuteId = orderExecute.getId();
        String orderId = orderExecute.getOrderId();

        if (StringUtils.isEmpty(orderExecuteId)) {
            orderExecute.setId(CodeTools.getCode32());
            orderExecuteService.insert(factory, orderExecute);
        } else {
            orderExecuteService.update(factory, orderExecute);
        }

        //工单执行备件信息
        List<OrderExecutePart> orderExecutePart = repairExecuteInfoVo.getOrderExecutePart();
        orderExecutePart.forEach(p -> {
            p.setExecuteId(orderExecute.getId());
            p.setOrderId(orderId);
        });

        orderExecutePartService.insertBatch(factory, orderExecutePart);

        return orderExecuteId;
    }

    @Override
    public boolean deleteExecuteInfo(SessionFactory factory, String orderId, String orderExecuteId) {

        //根据工单id及工单执行id 删除执行信息
        orderExecuteService.deleteOrderExecute(factory, orderId, orderExecuteId);

        orderExecutePartService.deleteOrderExecutePart(factory, orderId, orderExecuteId);

        return true;
    }

    @Override
    @TaskAnnotation("insertOrUpdateOperateForm")
    public String insertOrUpdateOperateForm(SessionFactory factory, OperateFormVo operateFormVo) {

        List<RepairReadyPo> repairReadyPoList = operateFormVo.getRepairReadyPoList();

        if (CollectionUtils.isEmpty(repairReadyPoList)) {
            return null;
        }
        String repairId = repairReadyPoList.get(0).getRepairId();

        //1.2 作业表单-存在问题,改进建议 入主表
        String opinion = operateFormVo.getOpinion();
        String problem = operateFormVo.getProblem();

        RepairPo repairPo = new RepairPo();
        repairPo.setId(repairId);
        repairPo.setOpinion(opinion);
        repairPo.setProblem(problem);
        repairService.update(factory, repairPo);

        //1.3 作业表单-作业准备 根据id更新确认状态值
        repairReadyService.updateBatch(factory, repairReadyPoList);

        //1.4 项目
        List<RepairItemVo> itemBeanVoList = operateFormVo.getItemBeanVoList();

        if (CollectionUtils.isEmpty(itemBeanVoList)) {
            List<RepairEquipmentVo> repairEquipmentVoList = operateFormVo.getRepairEquipmentVoList();
            if (CollectionUtils.isNotEmpty(repairEquipmentVoList)) {
                itemBeanVoList = new ArrayList<>();
                for (RepairEquipmentVo repairEquipmentVo : repairEquipmentVoList) {
                    List<RepairItemVo> itemVos = repairEquipmentVo.getItemVos();
                    if (CollectionUtils.isNotEmpty(itemVos)) {
                        itemBeanVoList.addAll(itemVos);
                    }
                }
            }
        }

        //处理一下itemBeanVoList -- 将子节点信息 平铺出来
        List<RepairItemVo> res = new ArrayList<>();
        dealItemBeanVoList(res, itemBeanVoList);

        //先根据工单id删除参数项
        itemParamsService.deleteByRepairId(factory,repairId);
        if (CollectionUtils.isNotEmpty(res)) {
            res.forEach(p -> {

                //1.4.1 项目备注及确认信息
                RepairItemPo repairItemPo = repairConvertor.voToRepairItemBean(p);
                itemService.update(factory, repairItemPo);

                List<RepairItemParamsPo> itemParamsBeanList = p.getItemParamsBeanList();
                List<OrderSignBean> orderSignBeanList = p.getOrderSignBeanList();
                //项目
                if (CollectionUtils.isNotEmpty(itemParamsBeanList)) {
                    itemParamsService.insertBatch(factory, itemParamsBeanList);
                }
                //工单签名
                if (CollectionUtils.isNotEmpty(orderSignBeanList)) {
                    orderSignService.insertBatch(factory, orderSignBeanList);
                }
            });
        }

        return repairId;
    }

    private void dealItemBeanVoList(List<RepairItemVo> list, List<RepairItemVo> itemBeanVoList) {

        if (CollectionUtils.isEmpty(itemBeanVoList)) {
            return;
        }

        for (RepairItemVo itemVo : itemBeanVoList) {

            List<RepairItemVo> repairItemVoList = itemVo.getRepairItemVoList();
            itemVo.setRepairItemVoList(null);
            list.add(itemVo);
            if (CollectionUtils.isNotEmpty(repairItemVoList)) {
                dealItemBeanVoList(list, repairItemVoList);
            }
        }

        return;
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        return 0;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public OperateFormVo queryByOrderId(SessionFactory factory, String orderId) {

        OperateFormVo operateFormVo = new OperateFormVo();

        //1.根据工单id查询作业表单

        //1.1 作业准备
        List<RepairReadyPo> repairReadyPoList = repairReadyService.queryByOrderId(factory, orderId);
        //1.2 作业过程
        List<RepairEquipmentVo> repairEquipmentVoList = equipmentService.queryByOrderId(factory, orderId);

        //1.3 作业指导书
        List<OrderInstructionBean> orderInstructionBeans = orderInstructionService.queryByBizId(factory, orderId);

        operateFormVo.setRepairReadyPoList(repairReadyPoList);
        operateFormVo.setRepairEquipmentVoList(repairEquipmentVoList);
        operateFormVo.setOrderInstructionBeanList(orderInstructionBeans);

        RepairPo repairPo = repairService.queryRepairBeanById(factory, orderId);

        operateFormVo.setOpinion(repairPo.getOpinion());
        operateFormVo.setProblem(repairPo.getProblem());

        return operateFormVo;

    }

    @Override
    @TaskAnnotation("queryReadyByOrderId")
    public List<RepairReadyPo> queryReadyByOrderId(SessionFactory factory, String orderId) {
        return repairReadyService.queryByOrderId(factory, orderId);
    }

    @Override
    @TaskAnnotation("queryInstructionByOrderId")
    public List<OrderInstructionBean> queryInstructionByOrderId(SessionFactory factory, String orderId) {
        return orderInstructionService.queryByBizId(factory, orderId);
    }

    @Override
    @TaskAnnotation("queryEquipmentVoByOrderId")
    public List<RepairEquipmentVo> queryEquipmentVoByOrderId(SessionFactory factory, String orderId) {
        return equipmentService.queryByOrderId(factory, orderId);
    }

    @Override
    @TaskAnnotation("queryEquipmentBeanListByOrderId")
    public List<RepairEquipmentPo> queryEquipmentBeanListByOrderId(SessionFactory factory, String orderId) {
        RepairEquipmentPo repairEquipmentPo = new RepairEquipmentPo();
        repairEquipmentPo.setRepairId(orderId);
        return equipmentService.queryList(factory, repairEquipmentPo);
    }

    @Override
    @TaskAnnotation("queryRepairItemByEqId")
    public List<RepairItemVo> queryRepairItemByEqId(SessionFactory factory, String orderId, String repairEquipmentId) {
        return itemService.queryByEquipmentId(factory, orderId, repairEquipmentId);
    }

    @Override
    @TaskAnnotation("updateRepairReady")
    public int updateRepairReady(SessionFactory factory, RepairReadyPo repairReadyPo) {
        //主要是更新确认状态
        return repairReadyService.update(factory, repairReadyPo);
    }

    @Override
    @TaskAnnotation("updateRepairItemVo")
    public int updateRepairItemVo(SessionFactory factory, RepairItemVo repairItemVo) {

        //设备下-维护项目
        RepairItemPo repairItemPo = repairConvertor.voToRepairItemBean(repairItemVo);
        //主要是修改状态,备注
        itemService.update(factory, repairItemPo);

        /*List<RepairItemParamsBean> itemParamsBeanList = repairItemVo.getItemParamsBeanList();
        //先删除当前项目数据记录项列表,再插入
        itemParamsService.insertBatch(factory, itemParamsBeanList);

        List<OrderSignBean> orderSignBeanList = repairItemVo.getOrderSignBeanList();
        //先删除当前项目签名人列表,再插入
        orderSignService.insertBatch(factory, orderSignBeanList);*/

        return 1;
    }

    @Override
    @TaskAnnotation("updateRepairItem")
    public int updateRepairItem(SessionFactory factory, RepairItemPo repairItemPo) {

        //主要是修改状态,备注
        itemService.update(factory, repairItemPo);

        return 1;
    }

    @Override
    @TaskAnnotation("updateRepairItemParams")
    public int updateRepairItemParams(SessionFactory factory, RepairItemParamsPo itemParamsBean) {
        //主要更新参数值
        return itemParamsService.update(factory, itemParamsBean);
    }

    @Override
    @TaskAnnotation("insertOrderSignInfo")
    public int insertOrderSignInfo(SessionFactory factory, OrderSignBean orderSignBean) {
        //新增签名人
        return orderSignService.insert(factory, orderSignBean);
    }

    @Override
    @TaskAnnotation("deleteOrderSignInfo")
    public int deleteOrderSignInfo(SessionFactory factory, String orderSignId) {
        //删除签名人记录
        return orderSignService.deleteById(factory, orderSignId);
    }

    @Override
    @TaskAnnotation("updateRepairInfo")
    public int updateRepairInfo(SessionFactory factory, RepairPo repairPo) {
        //主要更新改进建议和存在问题
        return repairService.update(factory, repairPo);
    }

    @Override
    @TaskAnnotation("generateStand")
    public boolean generateStand(SessionFactory factory, String orderId) {

        RepairEquipmentPo repairEquipmentPo = new RepairEquipmentPo();
        repairEquipmentPo.setRepairId(orderId);
        List<RepairEquipmentPo> repairEquipmentPos = equipmentService.queryList(factory, repairEquipmentPo);

        List<RepairDTO> repairDTOS = new ArrayList<>();

        List<RepairItemPo> repairItems = new ArrayList<>();
        List<RepairItemParamsPo> repairItemParams = new ArrayList<>();

        repairEquipmentPos.forEach(p -> {
            String repairId = p.getRepairId();
            String eqid = p.getId();
            String equipmentId = p.getEquipmentId();
            String standardId = p.getStandardId();
            Response<RepairDTO> rsp = repairStandService.select(standardId);
            if (rsp.getCode() == HttpStatus.OK.value()) {

                List<RepairItemPo> repairItemsTmp = new ArrayList<>();
                List<RepairItemParamsPo> repairItemParamsTmp = new ArrayList<>();

                RepairDTO data = rsp.getData();

                dealRepairDTOS(data, repairItemsTmp, repairItemParamsTmp);

                //分别入库
                //处理数据
                repairItemsTmp.forEach(v -> {
                    v.setEquipmentId(equipmentId);
                    v.setRepairEquipId(eqid);
                    v.setRepairId(repairId);
                });

                repairItemParamsTmp.forEach(v -> {
                    v.setEquipmentId(equipmentId);
                    v.setRepairEquipId(eqid);
                    v.setRepairId(repairId);
                });

                repairItems.addAll(repairItemsTmp);
                repairItemParams.addAll(repairItemParamsTmp);

            }
        });

        itemService.insertBatch(factory, repairItems);

        itemParamsService.deleteByRepairId(factory,orderId);
        itemParamsService.insertBatch(factory, repairItemParams);

        return true;
    }

    //标准处理
    private void dealRepairDTOS(RepairDTO repairDTO, List<RepairItemPo> repairItems, List<RepairItemParamsPo> repairItemParams) {
        if (repairDTO == null) {
            return;
        }

        List<RepairItemDTO> itemDTOs = repairDTO.getItemDTOs();
        dealRepairItemDTOS(null, itemDTOs, repairItems, repairItemParams);

    }


    //标准处理
    private void dealRepairItemDTOS(String parentId, List<RepairItemDTO> repairItemDTOS, List<RepairItemPo> repairItems, List<RepairItemParamsPo> repairItemParams) {

        if (CollectionUtils.isEmpty(repairItemDTOS)) {
            return;
        }
        repairItemDTOS.forEach(p -> {

            RepairItemPo repairItem = repairConvertor.dtoToBean(p);
            repairItem.setId(CodeTools.getCode32());
            repairItem.setParentId(parentId);
            repairItems.add(repairItem);
            List<RepairItemParams> itemParams = p.getItemParams();
            itemParams.forEach(v -> {
                RepairItemParamsPo itemParamsBean = repairConvertor.beanToBean(v);
                itemParamsBean.setItemId(repairItem.getId());
                itemParamsBean.setId(null);
                repairItemParams.add(itemParamsBean);
            });

            List<RepairItemDTO> children = p.getChildren();
            dealRepairItemDTOS(repairItem.getId(), children, repairItems, repairItemParams);
        });

    }

}
